package com.xmut.web;

import com.sirdc.modules.core.web.JsonBaseController;
import com.sirdc.modules.core.web.model.Message;
import com.sirdc.modules.sys.entity.SysUser;
import com.sirdc.modules.sys.security.SysPrincipal;
import com.sirdc.modules.sys.service.SysUserService;
import com.sirdc.modules.sys.util.SysUserUtils;
import com.sirdc.modules.utils.StringUtils;
import com.xmut.databean.FrontDatabean;
import com.xmut.databean.TopicListDatabean;
import com.xmut.entity.Comment;
import com.xmut.entity.Topic;
import com.xmut.entity.TopicPraise;
import com.xmut.filter.TopicFilter;
import com.xmut.service.CommentService;
import com.xmut.service.TopicPraiseService;
import com.xmut.service.TopicService;
import org.apache.commons.lang3.StringEscapeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 *
 */
@Controller
@RequestMapping("/topic")
public class FrontTopicController extends JsonBaseController {


    @Autowired
    private TopicService topicService;
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private CommentService commentService;
    @Autowired
    private TopicPraiseService topicPraiseService;

    @Override
    protected String getView(String view) {
        return "" + view;
    }

    /**
     * 发布话题
     * @param topic
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/pass/create", method = RequestMethod.POST)
    public Message create(@ModelAttribute Topic topic) {
        topic.setUserId(SysUserUtils.getUserId());
        topic.setClickNumber(0);
        topic.setCommentNumber(0);
        topic.setPraiseNumber(0);
        if (StringUtils.isBlank(topic.getTopicContent())) {
            return coverMessage("500","请输入话题内容");
        }
        topicService.save(topic);
        return coverMessage("200");
    }

    /**
     * 话题列表
     * @param filter
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/list",method = RequestMethod.POST)
    public Message list(@ModelAttribute TopicFilter filter) {
        filter.getOrder().put("updDate",TopicFilter.DESC);
        filter.getOrder().put("updTime",TopicFilter.DESC);
        List<Topic> topics = topicService.query(filter);
        List<TopicListDatabean> topicListDatabeans = new ArrayList<>();
        for (Topic topic : topics) {
            topic.setTopicContent(StringEscapeUtils.unescapeHtml4(topic.getTopicContent()));
            topic.setTopicName(StringEscapeUtils.unescapeHtml4(topic.getTopicName()));

            SysUser user = sysUserService.getById(topic.getUserId());
            if (user == null) {
                user = new SysUser();
                user.setName("");
            }
            TopicListDatabean databean = new TopicListDatabean(user.getName(),topic);
            topicListDatabeans.add(databean);

        }
        return coverMessage("200","", new FrontDatabean<TopicListDatabean>(filter.getPaging(),topicListDatabeans));
    }

    /**
     * 获取话题数
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/number", method = RequestMethod.GET)
    public Message number() {
        int number = topicService.queryNumber();
        return coverMessage("200","", number);
    }

    /**
     * 话题详情
     * @param id
     * @param model
     * @return
     */
    @RequestMapping(value = "/{id}/detail", method = RequestMethod.GET)
    public String  detail(@PathVariable Long id, Model model) {
        Topic topic = topicService.getById(id);
        topic.setClickNumber(topic.getClickNumber() == null? 1 : topic.getClickNumber()+ 1);
        topicService.update(topic);

        topic.setTopicContent(StringEscapeUtils.unescapeHtml4(topic.getTopicContent()));
        topic.setTopicName(StringEscapeUtils.unescapeHtml4(topic.getTopicName()));
        model.addAttribute("topic",topic);
        SysUser user = sysUserService.getById(topic.getUserId());
        model.addAttribute("user",user);

        SysPrincipal principal = SysUserUtils.getSysPrincipal();
        if (principal != null) {
            TopicPraise praise = topicPraiseService.getByUserIdWithTopicId(SysUserUtils.getUserId(), topic.getSysId());
            model.addAttribute("praise",praise);
        }
        return "/front/detail";
    }

    /**
     * 回复话题
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/pass/reply", method = RequestMethod.POST)
    public Message reply(@ModelAttribute Comment comment) {
        if(StringUtils.isBlank(comment.getComment())){
            return coverMessage("500","请输入评论内容！");
        }
        if(comment.getTopicId() == null){
            return coverMessage("500","参数错误");
        }
        commentService.saveComment(comment);

        return coverMessage("200");
    }

    /**
     * 话题点赞
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/pass/{topicId}/praise", method = RequestMethod.POST)
    public Message praise(@PathVariable Long topicId) {
        topicPraiseService.savePraise(topicId);
        return coverMessage("200");
    }


}
